<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入单据号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="scId" label-width="150px">
        <el-select v-model="queryParams.scId" placeholder="全部" clearable >
          <el-option v-for="(item, index) in ck" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预先盘点状态" prop="takeStatus" label-width="150px">
        <el-select v-model="queryParams.takeStatus" placeholder="全部" clearable >
          <el-option label="初盘" value="0" ></el-option>
          <el-option label="复盘" value="1" ></el-option>
          <el-option label="抽盘" value="2" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作时间" >
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="操作人" prop="createBy">
        <el-select v-model="queryParams.createBy" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in user" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:PreTakeStockSheet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sheet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sheetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据号" align="left" prop="code" width="200"/>
      <el-table-column label="仓库名称" align="left" prop="center.name" />
      <el-table-column label="预先盘点状态" align="left" prop="takeStatus" >
        <template slot-scope="scope">
          <span>{{ scope.row.takeStatus == 0 ? "初盘" : scope.row.takeStatus == 1 ? "复盘" : "抽盘"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="left" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="left" prop="createBy" />
      <el-table-column label="备注" align="left" prop="description" />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" fixed="right" width="200px">
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="text"
            @click="selectTakeStockSheet(scope.row)"
            v-hasPermi="['system:PurchaseOrder:edit']"
          >查看</el-button>
          <el-button
            size="medium"
            type="text"
            v-if="scope.row.status !=3"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:PreTakeStockSheet:edit']"
          >修改</el-button>
          <el-button
            size="medium"
            type="text"
            v-if="scope.row.status !=3"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:PreTakeStockSheet:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看弹窗 -->
    <el-dialog title="查看" :visible.sync="open" width="1000px" append-to-body>
      <div style="border: 1px gainsboro solid;padding: 10px 10px 10px 30px">
        <div style="display: inline-block;width: 300px;"><p>仓库：{{ this.form.center.name }}</p></div>
        <div style="display: inline-block;">
          预先盘点状态：
          <el-checkbox v-model="checkbox1">初盘</el-checkbox>
          <el-checkbox v-model="checkbox2">复盘</el-checkbox>
          <el-checkbox v-model="checkbox3">抽盘</el-checkbox>
        </div>
        <br>
        <div style="display: inline-block;width: 300px;"><p>操作人：{{ this.form.createBy }}</p></div>
        <div style="display: inline-block;width: 300px;"><p>操作时间：{{ this.form.createTime }}</p></div>
        <br>
        <div style="display: inline-block;width: 800px;">备注：<textarea style="width:70%;height: 50px;border-radius: 5px;vertical-align:middle;border: 1px gainsboro solid" v-model="this.form.description" disabled/></div>
      </div>
      <div style="border: 1px gainsboro solid;margin-top: 5px">
        <el-table v-loading="loading" :data="this.sheetDetailList" @selection-change="handleSelectionChange">
          <el-table-column label="商品编号" align="left" prop="baseDataProduct.code" width="100px"/>
          <el-table-column label="商品名称" align="left" prop="baseDataProduct.name" width="200px"/>
          <el-table-column label="单位" align="left" prop="baseDataProduct.unit" width="100px"/>
          <el-table-column label="规格" align="left" prop="baseDataProduct.spec" width="100px"/>
          <el-table-column label="商品类目" align="left" prop="baseDataProduct.baseDataProductCategory.name" width="100px"/>
          <el-table-column label="商品品牌" align="left" prop="baseDataProduct.baseDataProductBrand.name" width="100px"/>

          <el-table-column label="初盘数量" align="left" prop="firstNum" width="100px"/>
          <el-table-column label="复盘数量" align="left" prop="secondNum" width="100px"/>
          <el-table-column label="抽盘数量" align="left" prop="randNum" width="100px"/>
          <el-table-column label="复盘初盘差异数量" align="left" prop="" width="180">
            <template slot-scope="scope">
              <span>{{scope.row.firstNum == null || scope.row.secondNum == null ? 0: scope.row.firstNum - scope.row.secondNum}}</span>
            </template>
          </el-table-column>
          <el-table-column label="复盘抽盘差异数量" align="left" prop="" width="180">
            <template slot-scope="scope">
              <span>{{scope.row.secondNum == null || scope.row.randNum == null ? 0: scope.row.secondNum - scope.row.randNum }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSheet, getSheet, delSheet, addSheet, updateSheet } from "@/api/system/PreTakeStockSheet";
import { center, user } from "@/api/system/order";

export default {
  name: "Sheet",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存预先盘点单表格数据
      sheetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        scId: null,
        takeStatus: null,
        createBy: null,
        createTime: null,
        createMinTime: null,
        createMaxTime: null,
      },
      // 表单参数
      form: {
        center: {
          name:''
        },
      },
      // 仓库下拉框
      ck: [],
      // 用户(审核人、操作人、销售员)下拉框
      user: [],
      // 表单校验
      rules: {
        code: [
          { required: true, message: "单据号不能为空", trigger: "blur" }
        ],
        scId: [
          { required: true, message: "仓库ID不能为空", trigger: "blur" }
        ],
        takeStatus: [
          { required: true, message: "预先盘点状态不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "操作人不能为空", trigger: "blur" }
        ],
        createById: [
          { required: true, message: "创建人ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "操作时间不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" }
        ],
        updateById: [
          { required: true, message: "修改人ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ]
      },

      //盘点订单详情
      sheetDetailList: [],

      //盘点状态
      checkbox1:false,
      checkbox2:false,
      checkbox3:false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询库存预先盘点单列表 */
    getList() {
      this.loading = true;
      listSheet(this.queryParams).then(response => {
        this.sheetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      center(this.ck).then(response => {
        this.ck = response.rows;
      });
      user(this.user).then(response => {
        this.user = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        scId: null,
        takeStatus: null,
        description: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      this.$router.push({
        path:'/PreTakeStockSheet/add',
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.$router.push({
        path:'/PreTakeStockSheet/update',
        query:{
          id: id,
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSheet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSheet(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库存预先盘点单编号为"' + ids + '"的数据项？').then(function() {
        return delSheet(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sheet/export', {
        ...this.queryParams
      }, `sheet_${new Date().getTime()}.xlsx`)
    },


    // 查看按钮
    selectTakeStockSheet(row) {
      this.checkbox1=false;
      this.checkbox2=false;
      this.checkbox3=false;

      const id = row.id || this.ids
      getSheet(id).then(response => {
        this.form = response.data;
        this.sheetDetailList = response.data.tblPreTakeStockSheetDetailList;
        this.open = true;

        if (this.form.takeStatus == 0){
          this.checkbox1 = true;
        }
        if (this.form.takeStatus == 1){
          this.checkbox1 = true;
          this.checkbox2 = true;
        }
        if (this.form.takeStatus == 2){
          this.checkbox1 = true;
          this.checkbox2 = true;
          this.checkbox3 = true;
        }
      });
    },
  }
};
</script>
